import { cloneDeep } from 'lodash'
import { SETREAD, DELMSG } from './actionTypes'
let defaultState = {
  msgLists: [
    {
      content: '消息1',
      isRead: false
    },
    {
      content: '消息2',
      isRead: true
    },
    {
      content: '消息3',
      isRead: false
    },
    {
      content: '消息4',
      isRead: false
    }
  ]
}
// 取缓存 给 msgList赋初始值
const msgLists = localStorage.getItem('msgLists')
                  ?
                 JSON.parse(localStorage.getItem('msgLists'))
                  :
                  false

if(msgLists) {
  defaultState.msgLists = msgLists
}


const reducer = (state = defaultState, action) => {
  console.log(action);
  let newState = cloneDeep(state)
  switch (action.type) {
    case SETREAD:
      newState.msgLists[action.index].isRead = true
      break;
    case DELMSG:
      newState.msgLists.splice(action.index, 1)
      break;
    default:
      break;
  }
  localStorage.setItem('msgLists', JSON.stringify(newState.msgLists))
  return newState
}

export default reducer